

//
//  YDBuySection1Cell.m
//  PeiZiWangBei
//
//  Created by 巨彦新 on 2018/3/2.
//  Copyright © 2018年 LK-IOS. All rights reserved.
//

#import "YDBuySection1Cell.h"



@interface YDBuySection1Cell()<TextFieldClickReturnDelegate,UITextFieldDelegate>
/**
 股票名称
 */
@property (nonatomic, strong)UILabel *stockName;

/**
 提示图片
 */
@property (nonatomic, strong)UIImageView *questionImage;

/**
 搜索股票的背景
 */
@property (nonatomic, strong)UIView *searchView;
/**
 点击搜索股票
 */
@property (nonatomic, copy)UILabel *searchLbl;



/**
 当前价
 */
@property (nonatomic, strong)UILabel *wan;
/**
 按钮数组
 */
@property (nonatomic, strong)NSMutableArray *btnArr;

@end


@implementation YDBuySection1Cell

- (NSMutableArray *)btnArr
{
    if (!_btnArr) {
        _btnArr = [NSMutableArray new];
    }
    return _btnArr;
}

-(void)setMoneylist:(NSMutableArray *)moneylist
{
    _moneylist = moneylist;
    [self.btnArr removeAllObjects];
    [self addChooseBtn];
}

#pragma UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
  
    if(textField.text.length==4){
        
        if ([string isEqualToString:@""]) {
            
            return YES;
            
        }else
            
            return NO;
        
    }else
        
        return YES;
    
}
-(void)textFieldDidChange:(OttoTextField *)textField
{
    
    
    if (!self.moneylist.count) {
        return;
    }
    
    
    //    NSString *sub =  [[self.moneylist objectAtIndex:0] stringByReplacingOccurrencesOfString:@".00" withString:@""];
    //    NSString *sub1 =  [[self.moneylist objectAtIndex:1] stringByReplacingOccurrencesOfString:@".00" withString:@""];
    //    NSString *sub2 =  [[self.moneylist objectAtIndex:2] stringByReplacingOccurrencesOfString:@".00" withString:@""];
    //    NSString *sub3 =  [[self.moneylist objectAtIndex:3] stringByReplacingOccurrencesOfString:@".00" withString:@""];
    //
    //    if ([textField.text isEqualToString:sub] || textField.text.length == 0) {
    //        [self refreshBtnWithIndex:0];
    //    }else if ([textField.text isEqualToString:sub1])
    //    {
    //        [self refreshBtnWithIndex:1];
    //    }else if ([textField.text isEqualToString:sub2])
    //    {
    //        [self refreshBtnWithIndex:2];
    //    }else if ([textField.text isEqualToString:sub3])
    //    {
    //        [self refreshBtnWithIndex:3];
    //    }else
    //    {
    //        [self refreshBtnWithIndex:10];
    //    }
    
    __block BOOL isExitIndex;
    
    [self.moneylist enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *sb = [obj stringByReplacingOccurrencesOfString:@".00" withString:@""];
        
        if ([textField.text isEqualToString:sb]) {
            [self refreshBtnWithIndex:idx];
            isExitIndex = YES;
        }
        else if (textField.text.length == 0)
        {
            [self refreshBtnWithIndex:0];
            isExitIndex = YES;
        }
    }];
    
    if (!isExitIndex) {
        [self refreshBtnWithIndex:10];
    }
    
}
                        

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self stockName];
//        [self questionImage];
        [self searchView];
//        [self searchLbl];
       [self  moneyTextField];
       [self wan];
        
    }
    return self;
}


- (void)tapClick:(UITapGestureRecognizer *)tap
{
    
}
- (void)textFieldClickReturn:(OttoTextField *)textField
{
    [self hideKeyBoard];
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    
}
- (UILabel *)stockName
{
    if (!_stockName) {
        _stockName = [UILabel new];
        _stockName.text = @"策略金额";
        _stockName.textColor = [UIColor colorWithHexString:blueyGrey];
        _stockName.font = [UIFont systemFontOfSize:font14ScreenWRate *LKScreenW];
        [self.contentView addSubview:_stockName];
        [_stockName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(rate375(16));
            make.top.equalTo(self.contentView).offset(isPhoneX? rateX812(15) :rate750(15));
        }];
    }
    return _stockName;
}
-(UIImageView *)questionImage
{
    if (_questionImage == nil) {
        UIImageView *questtionImage = [[UIImageView alloc] init];
        [self.contentView addSubview:questtionImage];
        questtionImage.userInteractionEnabled = YES;
        [questtionImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.stockName.mas_right).offset(rate375(6));
            make.centerY.equalTo(self.stockName.mas_centerY);
        }];
        questtionImage.image = [UIImage imageNamed:@"zdqq-zhujie"];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
        [questtionImage addGestureRecognizer:tap];
        
        _questionImage = questtionImage;
    }
    return _questionImage;
}


-(UIView *)searchView
{
    if (!_searchView) {
        _searchView = [UIView new];
        _searchView.backgroundColor = [UIColor colorWithHexString:mainGray];
        CGFloat w = rate375(290);
        CGFloat h = isPhoneX ? rateX812(40) :rate750(40);
        [self.contentView addSubview:_searchView];
        [_searchView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(rate375(20));
            make.top.equalTo(self.contentView).offset(isPhoneX? rateX812(90) :rate750(90));
            make.width.equalTo(@(w));
            make.height.equalTo(@(h));
        }];
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
//        [_searchView addGestureRecognizer:tap];
        
    }
    return _searchView;
}
- (UILabel *)searchLbl
{
    if (!_searchLbl) {
        _searchLbl = [UILabel new];
        _searchLbl.text = @"最低20万起，5万整数倍";
        _searchLbl.textColor = [UIColor colorWithHexString:blueyGrey];
        _searchLbl.font = [UIFont systemFontOfSize:font14ScreenWRate *LKScreenW];
        [self.searchView addSubview:_searchLbl];
        [_searchLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.searchView);
        }];

    }
    return _searchLbl;
}

-(OttoTextField *)moneyTextField
{
    if (_moneyTextField == nil) {
        OttoTextField *accountTextField = [[OttoTextField alloc] init];
        [self.searchView addSubview:accountTextField];
        accountTextField.textColor = [UIColor colorWithHexString:blueyGrey];
        accountTextField.font = [UIFont systemFontOfSize:font14ScreenWRate * LKScreenW];
        accountTextField.placeholder = @"可手动输入，最低5万起，5万整数倍";
        accountTextField.backgroundColor = [UIColor clearColor];
        accountTextField.borderStyle = UITextBorderStyleNone;
        accountTextField.textAlignment = NSTextAlignmentCenter;
        accountTextField.keyboardType = UIKeyboardTypeNumberPad;
        [accountTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        
        [accountTextField setKeyboardType:KeyboardTypeNumber];
        [accountTextField setNumberKeyboardType:NumberKeyboardTypeNumber];
        accountTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
        
        accountTextField.textFieldClickReturnDelegate = self;
        accountTextField.delegate = self;
        accountTextField.returnKeyType =UIReturnKeyDone;
        
        [accountTextField mas_makeConstraints:^(MASConstraintMaker *make) {

               make.center.equalTo(self.searchView);
        }];
        accountTextField.tintColor = [UIColor blackColor];

        
        _moneyTextField = accountTextField;
    }
    return _moneyTextField;
}

-(UILabel *)wan
{
    if (!_wan) {
        _wan = [UILabel new];
        _wan.text = @"万元";
        _wan.textColor = [UIColor colorWithHexString:dark];
        _wan.font = [UIFont systemFontOfSize:font14ScreenWRate *LKScreenW];
        [self.contentView addSubview:_wan];
        [_wan mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.searchView.mas_centerY);
            make.left.equalTo(self.searchView.mas_right).offset(rate375(16));
        }];
        
    }
    return _searchLbl;
}

- (void)hideKeyBoard
{
    [self.contentView endEditing:YES];
}

- (void)addChooseBtn
{
    //整个按钮区域的宽度
    CGFloat buttonsViewW =  LKScreenW - rate375(40);
    //按钮间的横向间距
    CGFloat marginH = rate375(14);
    //按钮间的竖向间距
    CGFloat marginV = isPhoneX ? rateX812(13) : rate750(13);
    //按钮的高度
    CGFloat buttonH = isPhoneX ? rateX812(24) : rate750(24);
    
    
    CGFloat y = isPhoneX ? rateX812(50) : rate750(50);
    
    for (int i = 0; i < self.moneylist.count; i ++) {
        
        //每行显示的个数
        NSInteger cows = 4;
        //按钮的宽度
        CGFloat buttonW = (buttonsViewW - (cows - 1)*marginH) / cows;
        
        //确定第几列第几行
        NSInteger whichCow = i / cows;
        NSInteger whichCol = i % cows;
        
        //确定按钮的x  y
        
        CGFloat buttonX = whichCol * (marginH + buttonW );
        CGFloat buttonY = whichCow * (marginV + buttonH );
        
        SYNoHeightedButton *button = [self createBtnWithFundModel:[self.moneylist objectAtIndex:i]];
        
        
        if (i == 0) {
            button.selected = YES;
            button.layer.borderColor = [UIColor clearColor].CGColor;
        }
        button.frame = CGRectMake(rate375(20) + buttonX, buttonY + y , buttonW, buttonH);
        
        [self.contentView addSubview:button];
        
        button.tag = i + 100;
        
        [self.btnArr addObject:button];
        
    }
}

- (SYNoHeightedButton *)createBtnWithFundModel:(NSString *)wan
{
    SYNoHeightedButton *button =  [SYNoHeightedButton buttonWithType:UIButtonTypeCustom];
    button.titleLabel.font = [UIFont systemFontOfSize:font12ScreenWRate * LKScreenW];
    button.titleLabel.adjustsFontSizeToFitWidth = YES;
    button.backgroundColor = [UIColor clearColor];
    // NSString *wan =   [model.value substringWithRange:NSMakeRange(0, model.value.length - 4)];
    [button setTitle:[NSString stringWithFormat:@"%@ 万",  [wan stringByReplacingOccurrencesOfString:@".00" withString:@""]] forState:UIControlStateNormal];
    
    //按钮颜色
    [button setTitleColor:[UIColor colorWithHexString:blueyGrey] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    //背景
    [button setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:fadedOrange]] forState:UIControlStateSelected];
    //边框
    button.layer.cornerRadius = 3.0f;
    button.layer.masksToBounds = YES;
    button.layer.borderWidth = 1.0f;
    button.layer.borderColor = [UIColor colorWithHexString:blueyGrey].CGColor;
    [button addTarget:self action:@selector(fundBtnDidSelected:) forControlEvents:UIControlEventTouchUpInside];
    
    
    return button;
}


- (void)fundBtnDidSelected:(UIButton *)sender
{
    [self.btnArr enumerateObjectsUsingBlock:^(UIButton * obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.selected = NO;
        obj.layer.borderColor = [UIColor colorWithHexString:blueyGrey].CGColor;
    }];
    
    
    self.moneyTextField.text = [sender.titleLabel.text stringByReplacingOccurrencesOfString:@" 万" withString:@""];
    sender.selected  =YES;
    
    sender.layer.borderColor = [UIColor clearColor].CGColor;
    
    if (self.clickBack) {
        self.clickBack(sender.tag - 100);
    }
}
- (void)refreshBtnWithIndex:(NSInteger)index
{
    
    [self.btnArr enumerateObjectsUsingBlock:^(UIButton * obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.selected = NO;
        obj.layer.borderColor = [UIColor colorWithHexString:blueyGrey].CGColor;
    }];
    if (index == 10) {
        return;
    }
    UIButton *btn = [self.btnArr objectAtIndex:index];
    btn.selected = YES;
    btn.layer.borderColor = [UIColor clearColor].CGColor;
}

@end
